from rest_framework import serializers
from .models import *

# 规格管理的序列化器
class PecificationSer(serializers.ModelSerializer):
    # 名字， 仅仅用于序列化输出
    spu_name = serializers.SerializerMethodField(read_only=True)

    def get_spu_name(self, obj):
        # obj:模型类对象
        return obj.spu.name

    class Meta:
        model = SPUSpecification
        fields = "__all__"



# 规格选项名称序列化
class SpecificationOptionSer(serializers.ModelSerializer):
    # 类别名字， 仅仅用于序列化输出
    spec_name = serializers.SerializerMethodField(read_only=True)

    def get_spec_name(self, obj):
        # obj:类别组对象
        return obj.spec.name

    spu_spec = serializers.SerializerMethodField(read_only=True)

    def get_spu_spec(self, obj):
        # obj:对象
        return obj.spec.id


    class Meta:
        model = SpecificationOption
        fields = "__all__"

class SKUSpecificationSer(serializers.ModelSerializer):
    # sku_name = serializers.SerializerMethodField(read_only=True)
    # def get_sku_name(self, obj):
    #     # obj:类别组对象
    #     return obj.sku.name
    #
    # spec_spec = serializers.SerializerMethodField(read_only=True)
    #
    # def get_spec_spec(self, obj):
    #     # obj:对象
    #     return obj.spec.name
    #
    # option = serializers.SerializerMethodField(read_only=True)
    #
    # def get_option(self, obj):
    #     # obj:对象
    #     return obj.option.value


    class Meta:
        model = SKUSpecification
        fields = "__all__"

